﻿using System;
using MbUnit.Framework;
using TimeStamper.DSL;

namespace TimeStamper.Tests
{
    [TestFixture]
    public class ParserFixtures
    {
        private static DateTime Today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

        [Test]
        public void Can_Parse_Today_QuickDate()
        {
            var dateRange = QuickParser.Eval("Today");
            Assert.AreEqual(dateRange.Dates[0], Today);
        }

        [Test]
        public void Can_Parse_Tommorow_QuickDate()
        {
            var dateRange = QuickParser.Eval("Tommorow");
            Assert.AreEqual(dateRange.Dates[0], Today.AddDays(1));
        }

        [Test]
        public void Can_Parse_Yesturday_QuickDate()
        {
            var dateRange = QuickParser.Eval("Yesterday");
            Assert.AreEqual(dateRange.Dates[0], Today.AddDays(-1));
        }

        [Test]
        public void Can_Parse_DayIdentification_QuickDate()
        {
            var dateRange = QuickParser.Eval("tuesday");
            Assert.IsTrue(dateRange.Dates[0].DayOfWeek == (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "tuesday", true));
        }

        [Test]
        public void Can_Parse_X_Days_QuickIdentification()
        {
            var dateRange = QuickParser.Eval("4 days");
            Assert.AreEqual(dateRange.Dates[0], Today.AddDays(4));
        }

        [Test]
        public void Can_Parse_X_Months_QuickIdentification()
        {
            var dateRange = QuickParser.Eval("2 Months");
            Assert.AreEqual(dateRange.Dates[0], Today.AddMonths(2));
        }

        [Test]
        public void Can_Parse_X_Year_QuickIdentification()
        {
            var dateRange = QuickParser.Eval("6 Year");
            Assert.AreEqual(dateRange.Dates[0], Today.AddYears(6));
        }
        [Test]
        public void Can_Parse_X_Week_QuickIdentification()
        {
            var dateRange = QuickParser.Eval("3 Weeks");
            Assert.AreEqual(dateRange.Dates[0], Today.AddDays(3*7));
        }

        [Test]
        public void Ago_Keyword_Inverts_X_QuickIdentifications()
        {
            var dateRange = QuickParser.Eval("3 Days Ago");
            Assert.AreEqual(dateRange.Dates[0], Today.AddDays(-3));
        }

        [Test]
        public void Can_Parse_Next_QuickIdentification()
        {
            Assert.AreEqual(QuickParser.Eval("Next Day").Dates[0], Today.AddDays(1));
            Assert.AreEqual(QuickParser.Eval("Next Month").Dates[0], Today.AddMonths(1));
            Assert.AreEqual(QuickParser.Eval("Next Year").Dates[0], Today.AddYears(1));
        }

        [Test]
        public void Can_Parse_Previous_QuickIdentification()
        {
            Assert.AreEqual(QuickParser.Eval("Previous Day").Dates[0], Today.AddDays(-1));
            Assert.AreEqual(QuickParser.Eval("Previous Month").Dates[0], Today.AddMonths(-1));
            Assert.AreEqual(QuickParser.Eval("Previous Year").Dates[0], Today.AddYears(-1));
        }

        [Test]
        public void Can_Parse_MonthDateIdentification()
        {
            Assert.AreEqual(QuickParser.Eval("15th").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15));
            Assert.AreEqual(QuickParser.Eval("15th july").Dates[0], new DateTime(DateTime.Now.Year, 7, 15));
            Assert.AreEqual(QuickParser.Eval("2nd december").Dates[0], new DateTime(DateTime.Now.Year, 12, 2));
        }

        [Test]
        public void Can_Parse_Month_AND_Year_DateIdentification()
        {
            Assert.AreEqual(QuickParser.Eval("15th 2010").Dates[0], new DateTime(2010, DateTime.Now.Month, 15));
            Assert.AreEqual(QuickParser.Eval("15th july 2010").Dates[0], new DateTime(2010, 7, 15));
            Assert.AreEqual(QuickParser.Eval("2nd december 2010").Dates[0], new DateTime(2010, 12, 2));

            Assert.AreEqual(QuickParser.Eval("Previous Month 2012").Dates[0], new DateTime(2012, Today.AddMonths(-1).Month, Today.Day));
        }

        [Test]
        public void Can_Parse_Time_Identification()
        {
            Assert.AreEqual(QuickParser.Eval("Today at 2pm").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 0,0));
            Assert.AreEqual(QuickParser.Eval("Tommorow at 2:30pm").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 30, 0).AddDays(1));
            Assert.AreEqual(QuickParser.Eval("Yesterday at 2pm").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 0, 0).AddDays(-1));
            Assert.AreEqual(QuickParser.Eval("Tommorow at 14:30").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 30, 0).AddDays(1));
            Assert.AreEqual(QuickParser.Eval("Previous Year at 2am").Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 2, 0, 0).AddYears(-1));
            Assert.AreEqual(QuickParser.Eval("15th july at 2am").Dates[0], new DateTime(DateTime.Now.Year, 7, 15, 2, 0, 0));
            Assert.AreEqual(QuickParser.Eval("15th july 2010 at 2am").Dates[0], new DateTime(2010, 7, 15, 2, 0, 0));
        }

        [Test]
        public void Can_Parse_TO_Dates()
        {
            var dateRange = QuickParser.Eval("Today at 2pm - 15th july 2010 at 2am");
            Assert.AreEqual(dateRange.Dates[0], new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 0, 0));
            Assert.AreEqual(dateRange.Dates[1], new DateTime(2010, 7, 15, 2, 0, 0));
        }

        [Test]
        public void Can_Parse_Multipule_TO_Dates()
        {
            var dateRange = QuickParser.Eval("Today at 2pm - 15th july 2010 at 2am To Tommorow at 14:30");
            Assert.AreEqual(3, dateRange.Dates.Count);
        }

    }
}